//
//  UUNotificationManager.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 23/01/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUNotificationManager.h"
#import <UUFoundation/DKDataManager.h>
#import "UUNotificationListRequestModel.h"
#import "UUNotificationUnreadCountRequestModel.h"

@interface UUNotificationManager ()

@property (nonatomic, strong) UUNotificationUnreadCountModel *unreadCountModel;
@property (nonatomic, assign) BOOL isRequesting;

@end

@implementation UUNotificationManager

- (void)asyncFetchNotificationsWithType:(UUNotificationType)type
                                  since:(id)since
                                 length:(int)length
                          completeBlock:(void (^)(NSArray<UUNotificationModel *> *notifications, id since, NSError *error))completeBlock {
    UUNotificationListRequestModel *requestModel = [UUNotificationListRequestModel new];
    requestModel.type = type;
    requestModel.since = since;
    requestModel.perPage = length;
    
    [[DKDataManager new] requestWithRequestModel:requestModel completedBlock:^(DKResponseModel *responseModel, NSError *error) {
        if (error == nil) {
            UUPagingResponseModel *pagingResponseModel = (UUPagingResponseModel *)responseModel;
            BLOCK_SAFE_CALLS_In_Main_Queue(completeBlock, pagingResponseModel.insert, pagingResponseModel.since, nil);
        } else {
            BLOCK_SAFE_CALLS_In_Main_Queue(completeBlock, nil, nil, error);
        }
    }];
}

- (void)asyncUpdateUnreadCount {
    if (self.isRequesting) {
        return;
    } else {
        self.isRequesting = YES;
    }
    
    UUNotificationUnreadCountRequestModel *requestModel = [UUNotificationUnreadCountRequestModel new];
    WEAK_SELF();
    [[DKDataManager new] requestWithRequestModel:requestModel completedBlock:^(DKResponseModel *responseModel, NSError *error) {
        weakSelf.isRequesting = NO;
        
        UUNotificationUnreadCountModel *unreadCountModel = nil;
        if (error == nil) {
            NSInteger messageUnreadCount = [responseModel.data[0] integerValue];
            NSInteger productUnreadCount = [responseModel.data[1] integerValue];
            NSInteger newsUnreadCount = [responseModel.data[2] integerValue];
            
            unreadCountModel = [UUNotificationUnreadCountModel new];
            unreadCountModel.message = messageUnreadCount;
            unreadCountModel.product = productUnreadCount;
            unreadCountModel.news = newsUnreadCount;
            
            weakSelf.unreadCountModel = unreadCountModel;
        }
        
        [weakSelf notifyObserversWithSelector:@selector(notificationManagerDidUpdateUnreadCount) withObject:nil];
    }];
}

- (UUNotificationUnreadCountModel *)fetchNotificationUnreadCount {
    return self.unreadCountModel;
}

- (void)clearUnreadCountForMessage {
    self.unreadCountModel.message = 0;
    
    [self notifyObserversWithSelector:@selector(notificationManagerDidUpdateUnreadCount) withObject:nil];
}

- (void)clearUnreadCountForProduct {
    self.unreadCountModel.product = 0;
    
    [self notifyObserversWithSelector:@selector(notificationManagerDidUpdateUnreadCount) withObject:nil];
}

- (void)clearUnreadCountForNews {
    self.unreadCountModel.news = 0;
    
    [self notifyObserversWithSelector:@selector(notificationManagerDidUpdateUnreadCount) withObject:nil];
}

@end
